<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/ajax.php';

class foreground_cart_add_action_service extends foreground_ajax_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {	     		     
	    //var_dump(__METHOD__); exit();
	    //var_dump($_SESSION); exit();
	
	    $result = array('status' => 0, 'message' => '', 'data' => array());
	
	    //var_dump($_POST); exit();	  
	    //$good_id = 0;
	    $good_id = init_user_data_key($_POST, 'good_id', 'int');
	    $good_number = init_user_data_key($_POST, 'good_number', 'int');
	    $spec_id = init_user_data_key($_POST, 'spec_id', 'int');
	     
	    $action_data = wwy_action::$data;
	    $session_member_info = $action_data['session_member_info'];
	    $member_id = $session_member_info['id'];
		        
        if($good_id < 1){
            $result['status']  = -2;
            $result['message'] = '加入购物车失败';
        }else if($good_number < 1){
            $result['status']  = -3;
            $result['message'] = '加入购物车失败';
        }else if($spec_id < 1){
            $result['status']  = -3;
            $result['message'] = '加入购物车失败';
        }
	    //var_dump($result); exit();
	    //exit();
	
        if($result['status'] == 0){
            $model_foreground_member = wwy_resource_container::get_model_foreground_member();
            //var_dump($model_foreground_member); exit();
            $where   = array('id' => $member_id, 'status' => '1');
	        $member  = $model_foreground_member->fetch_row($where);
            //var_dump($member); exit();
            if(empty($member)){
                $result['status']  = -4;
                $result['message'] = '不存在该用户，或该用户未激活';
            }
        }
	    
	    $spec_desc = '';
	    if($result['status'] == 0){
	        $model_foreground_good = wwy_resource_container::get_model_foreground_good();
	        $model_foreground_cart = wwy_resource_container::get_model_foreground_cart();
	        
	        $if_spec_item_simple = false;
	        $check_good_spec_result = $model_foreground_cart->check_good_spec($good_id, $spec_id, $if_spec_item_simple);
	        //var_dump($check_good_spec_result); exit();
	        if($check_good_spec_result['status'] < 0){
	            $result['status']  = $check_good_spec_result['status'];
	            $result['message'] = '加入购物车失败。' . $check_good_spec_result['message'];
	        }else{
	            $good_info = $check_good_spec_result['data']['good_info'];
	            
	            $spec_item = $check_good_spec_result['data']['spec_item'];
	            $spec_desc = $spec_item['spec_desc'];
	        }
	    }
	    
	    if($result['status'] == 0){	        
	        //$model_foreground_cart = $this->_resource_container->get_model_foreground_cart();
	        //var_dump($model_foreground_cart); exit();
	        $where      = array(
	            'member_id' => $member_id,
	            'good_id'   => $good_id,
	            'spec_id'   => $spec_id
	        );
	        $cart_row = $model_foreground_cart->fetch_row($where);
	        //var_dump($cart_row); exit();
	         
	        if(empty($cart_row)){
	            /* 不存在，新增 */
	            
	            $match_spec_number_grade_price = $model_foreground_good->get_match_spec_number_grade_price($good_id, $spec_id, $good_number);
	            if(empty($match_spec_number_grade_price)){
	                $result['status']  = -400;
	                $result['message'] = '加入购物车失败。该商品规格购物车数量为' . $good_number . '，没有匹配的数量范围';
	            }else{
	                $good_price = $match_spec_number_grade_price['price'];
	               	            
    	            $cart_row_options = array(
    	                'member_id' => $member_id,
    	                'good_id'   => $good_id,
    	                'spec_id'   => $spec_id,
    	                'good_name'   => $good_info['name'],
    	                'spec_desc'   => $spec_desc,
    	                //'good_price'  => number_format(floatval($good_price), 2, '.', ''),
    	                'good_price'  => $good_price,
    	                'good_number' => $good_number,
    	                'good_thumb' => $good_info['image_thumb'][0],
    	            );
    	            
    	            $save_result = $model_foreground_cart->add($cart_row_options);
    	            //var_dump($save_result); exit();
	            }
	        }else{
	            /* 存在，更新 */
	            
	            $good_number = $cart_row['good_number'] + $good_number;
	            $match_spec_number_grade_price = $model_foreground_good->get_match_spec_number_grade_price($good_id, $spec_id, $good_number);
	            if(empty($match_spec_number_grade_price)){
	                $result['status']  = -400;
	                $result['message'] = '加入购物车失败。该商品规格购物车数量更新为' . $good_number . '，没有匹配的数量范围';
	            }else{
	                $good_price = $match_spec_number_grade_price['price'];
	                    	            
    	            $cart_row_options = array(
    	                'id'          => $cart_row['id'],
    	                'good_number' => $good_number,
    	                'good_price'  => $good_price,
    	            );
    	            //var_dump($cart_row_options); exit();
    	            
    	            $save_result = $model_foreground_cart->update_good_number($cart_row_options);
    	            //var_dump($save_result); exit();
	            }
	        }	        
	        //var_dump($save_result); exit();
	    
	        if($result['status'] == 0){
    	        if(!$save_result){
    	            $result['status']  = -6;
    	            $result['message'] = '加入购物车失败';
    	        }else{
    	            $result['status']  = 1;
    	            $result['message'] = '加入购物车成功';
    	        }
	        }
	    }
	
	    self::set_view_status($result['status']);
	    self::set_view_message($result['message']);
    }
}